﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Convertor.Interface;
using Wpf.CommonTools.StaticExtension;
using mshtml;
using Wpf.CommonTools.SystemExtension;
using Wpf.CommonTools;

namespace Convertor.Plugin.WebDownloader
{

    /// <summary>
    /// ConfigForm.xaml 的交互逻辑
    /// </summary>
    public partial class ConfigForm : PluginConfigForm
    {
        List<EncodingContainer> encodingList;

        public List<EncodingContainer> EncodingList
        {
            get { return encodingList ?? (encodingList = EncodingHelper.GetList(UILanguage.FindResource("Plugin_WebDownloader_Automatic"))); }
        }

        public Array FindTypeList
        {
            get { return Enum.GetValues(config.FindType.GetType()); }
        }

        public ConfigForm()
        {

            InitializeComponent();
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
        }

        private void tbUrl_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Uri url;
                bool b = Uri.TryCreate(((TextBox)sender).Text, UriKind.Absolute, out url);
                if (!b)
                {
                    b = Uri.TryCreate("http://" + ((TextBox)sender).Text, UriKind.Absolute, out url);
                }
                if (b)
                {
                    wbExplorer.Navigate(url);
                }
            }
        }

        private void wbExplorer_LoadCompleted(object sender, NavigationEventArgs e)
        {

        }

        private void tbUrl_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
        {
            ((TextBox)sender).ShowDropList(InputHistory);
        }

        private void wbExplorer_Loaded(object sender, RoutedEventArgs e)
        {
            //wbExplorer.Navigate("about:blank");
        }

        private void wbExplorer_Navigated(object sender, NavigationEventArgs e)
        {
            WebBrowser wb = (WebBrowser)sender;
            ConfigContent c = ((ConfigContent)Config);
            var doc = (mshtml.IHTMLDocument2)wb.Document;
            c.Cookie = doc.cookie;
            c.UrlReferrer = doc.referrer;
            tbUrl.Text = wb.Source.ToString();
            //c.Accept = wb.he
        }

        private void wbExplorer_Navigating_1(object sender, NavigatingCancelEventArgs e)
        {
            ConfigContent c = ((ConfigContent)Config);
            if (e.WebRequest != null)
            {
                c.UserAgent = e.WebRequest.Headers["UserAgent"];
            }
        }



        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) == null ? new ConfigContent() : (ConfigContent)((ICloneable)value).Clone(); }
        }

        #endregion

        #region IConfigFormEvent 成员

        public override object GetConfig()
        {
            //config.EncodingContent = cbEncoding.SelectedValue as EncodingContainer;
            //config.FindType = (FindTypes)Enum.Parse(typeof(FindTypes), cbGetTypes.SelectedValue.ToString());
            //config.Cookie = tbCookie.Text;
            //config.UrlReferrer = tbUrlReferrer.Text;
            //Config = config;
            return ((ConfigContent)Config).Clone();
        }

        #endregion

        #region IPluginHistory 成员

        public override object Input { get; protected set; }

        #endregion

    }
}
